package cn.yeziji.forum.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付文件前缀枚举
 *
 * @author hwy
 * @since 2023/07/29 12:29
 **/
@Getter
@AllArgsConstructor
public enum PayFilePrefixEnum {
  WECHAT(0, "http://merchant.yeziji.xyz/wechat", "wechat","/wechat", new String[]{"apiclient_cert.p12", "apiclient_cert.pem", "apiclient_key.pem"}),
  ;

  final int index;
  final String ossPrefix;
  final String ossSavePath;
  final String relativePrefix;
  final String[] requiredFiles;

  public List<String> getRelativeRequiredFilesPath() {
    return getRelativeRequiredFilesPath(this);
  }

  public String getUserOssPaySavePath(final Integer userId) {
    return this.getOssSavePath() + "/" + userId;
  }

  public String getUserRelativePayPrefixPath(final String rootPath, final Integer userId) {
    if (rootPath.lastIndexOf(File.separator) == -1) {
      return rootPath + File.separator + this.getRelativePrefix() + "/" + userId;
    }
    return rootPath + this.getRelativePrefix() + "/" + userId;
  }

  public String getUserRelativePayPrefixPath(final Integer userId) {
    return this.getRelativePrefix() + "/" + userId;
  }

  public static List<String> getRelativeRequiredFilesPath(PayFilePrefixEnum payFilePrefixEnum) {
    String[] files = payFilePrefixEnum.getRequiredFiles();
    if (files != null && files.length > 0) {
      return Arrays.stream(files)
          .map(file -> payFilePrefixEnum.getRelativePrefix() + File.separator + file)
          .collect(Collectors.toList());
    }
    return Collections.emptyList();
  }
}
